package main

import "fmt"

//deffer|类似于 java中的 finally
func main() {
	//最上面的 defer最后执行
	defer fmt.Println("main hello go 1")
	defer fmt.Println("main end 1")
	fmt.Println("main hello go 2")

	defer fun1()
	defer fun2()
	defer fun3()

	//defer 后执行，return先执行
	returnAndDefer()
}
func deferFun() int {
	fmt.Println("defer fun called")
	return 0
}
func returnFun() int {
	fmt.Println("returnFun fun called")
	return 2
}
func returnAndDefer() int {
	defer deferFun()
	return returnFun()
}
func fun1() {
	fmt.Println("1")
}
func fun2() {
	fmt.Println("2")
}
func fun3() {
	fmt.Println("3")
}
